import java.util.*;
import java.io.*;

public class UserService {
    private List<User> users = new ArrayList<>();
    private static final String USER_FILE = "data/users.txt";
    
    public UserService() {
        loadUsers();
        // 如果没有数据，添加默认用户
        if (users.isEmpty()) {
            users.add(new Admin("1", "admin", "ynuinfo#777", "12345678901"));
            users.add(new Manager("2", "manager1", "123456", "12345678902"));
            users.add(new Clerk("3", "clerk1", "123456", "12345678903"));
            saveUsers();
        }
    }
    
    public List<User> getUsers() {
        return users;
    }
    
    public User login(String username, String password) {
        for (User u : users) {
            if (u.getUsername().equals(username) && u.getPassword().equals(password)) {
                return u;
            }
        }
        return null;
    }
    
    // 添加用户
    public void addUser(User user) {
        users.add(user);
        saveUsers();
    }
    
    // 删除用户
    public void deleteUser(String id) {
        users.removeIf(u -> u.getId().equals(id));
        saveUsers();
    }
    
    // 修改用户
    public void updateUser(User user) {
        for (int i = 0; i < users.size(); i++) {
            if (users.get(i).getId().equals(user.getId())) {
                users.set(i, user);
                saveUsers();
                break;
            }
        }
    }
    
    // 文件持久化方法
    private void saveUsers() {
        try {
            File dataDir = new File("data");
            if (!dataDir.exists()) {
                dataDir.mkdirs();
            }
            
            FileWriter writer = new FileWriter(USER_FILE);
            for (User user : users) {
                writer.write(String.format("%s|%s|%s|%s|%s\n",
                    user.getId(), user.getUsername(), user.getPassword(),
                    user.getPhone(), user.getRole()));
            }
            writer.close();
        } catch (IOException e) {
            System.out.println("保存用户数据失败: " + e.getMessage());
        }
    }
    
    private void loadUsers() {
        try {
            File file = new File(USER_FILE);
            if (!file.exists()) {
                return;
            }
            
            Scanner fileScanner = new Scanner(file);
            while (fileScanner.hasNextLine()) {
                String line = fileScanner.nextLine();
                String[] parts = line.split("\\|");
                if (parts.length >= 5) {
                    String id = parts[0];
                    String username = parts[1];
                    String password = parts[2];
                    String phone = parts[3];
                    String role = parts[4];
                    
                    User user = null;
                    switch (role) {
                        case "admin":
                            user = new Admin(id, username, password, phone);
                            break;
                        case "manager":
                            user = new Manager(id, username, password, phone);
                            break;
                        case "clerk":
                            user = new Clerk(id, username, password, phone);
                            break;
                    }
                    if (user != null) {
                        users.add(user);
                    }
                }
            }
            fileScanner.close();
        } catch (IOException e) {
            System.out.println("加载用户数据失败: " + e.getMessage());
        }
    }
} 